import 'package:cbec_app/components/common/dashed_line.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import '../../../utils/Tools.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class ExpressDetailInfo extends StatefulWidget {
  const ExpressDetailInfo({super.key});

  @override
  State<ExpressDetailInfo> createState() => _ExpressDetailInfoState();
}

class _ExpressDetailInfoState extends State<ExpressDetailInfo> {
  late Map orderInfo;
  late String expressName;
  late String expressCode;
  late Map expressInfo;
  late List expressItems;
  late String receiveAddress;

  @override
  void initState() {
    super.initState();
    orderInfo = Get.arguments as Map;
    expressName = orderInfo['expressName'];
    expressCode = orderInfo['expressCode'];
    receiveAddress = orderInfo['receiveAddress'];
    expressInfo = orderInfo['expressInfo'];
    expressItems = List<Map>.from(expressInfo[expressCode]);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        elevation: 0,
        centerTitle: true, // 让标题居中
        leading: IconButton(
          icon: Icon(Icons.arrow_back_ios, color: Tools.fromHex('#000000')), // 自定义返回按钮
          onPressed: () {
            Navigator.pop(context); // 返回上一个页面
          },
        ),
        title: Text(
          '物流信息',
          style: TextStyle(
            color: Tools.fromHex('#000000'),
            fontSize: 16,
            fontWeight: FontWeight.bold,
          ),
        ),
      ),
      body: SingleChildScrollView(
        // Wrap the body with SingleChildScrollView
        child: Column(
          children: [
            _buildTopWidget(),
            Container(
              margin: const EdgeInsets.symmetric(horizontal: 10, vertical: 0),
              padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 0),
              decoration: const BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.all(Radius.circular(10)),
              ),
              child: Row(
                children: [
                  Container(
                    width: 80,
                  ),
                  SizedBox(
                    width: ScreenUtil().screenWidth - 120,
                    child: Row(
                      children: [
                        Stack(
                          children: [
                            Positioned(
                              top: 0,
                              bottom: 0,
                              child: Column(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: [
                                  const Expanded(child: SizedBox()),
                                  Container(
                                    alignment: Alignment.center,
                                    width: 24, // 圆形背景的宽度
                                    height: 24, // 圆形背景的高度
                                    decoration: const BoxDecoration(
                                      color: Colors.blue, // 背景颜色
                                      shape: BoxShape.circle, // 圆形
                                    ),
                                    child: Text(
                                      '收',
                                      style: TextStyle(
                                        color: Tools.fromHex('#F5FCFF'), // 文字颜色
                                        fontWeight: FontWeight.normal,
                                      ),
                                    ),
                                  ),
                                  const Expanded(child: DashedLind(axis: Axis.vertical)),
                                ],
                              ),
                            ),
                            Container(
                              width: ScreenUtil().screenWidth - 140,
                              margin: const EdgeInsets.symmetric(vertical: 20),
                              padding: const EdgeInsets.only(left: 30),
                              child: Text(
                                style: const TextStyle(
                                  color: Colors.black,
                                  fontSize: 16,
                                ),
                                '$receiveAddress',
                                softWrap: true, // 显式设置为 true，允许换行
                              ),
                            ),
                          ],
                        )
                      ],
                    ),
                  ),
                ],
              ),
            ),
            _buildListWidget(),
          ],
        ),
      ),
    );
  }

  /// 列表
  Widget _buildListWidget() {
    return ListView.builder(
      physics: const NeverScrollableScrollPhysics(), // Disable scrolling on the ListView
      shrinkWrap: true, // Use this to make the ListView take only the space it needs
      itemCount: expressItems.length,
      itemBuilder: (context, index) {
        var item = expressItems[index];
        return Container(
          margin: const EdgeInsets.symmetric(horizontal: 10, vertical: 0),
          padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 0),
          decoration: const BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.all(Radius.circular(10)),
          ),
          child: Row(
            children: [
              SizedBox(
                width: 80,
                child: Text(
                  item['time'],
                  textAlign: TextAlign.right, // 右对齐
                ),
              ),
              SizedBox(
                width: ScreenUtil().screenWidth - 120,
                child: Row(
                  // mainAxisAlignment: MainAxisAlignment.start,
                  // crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Stack(
                      children: [
                        Positioned(
                          left: 11.5,
                          top: 0,
                          bottom: 0,
                          child: Container(
                            width: 1,
                            color: Tools.fromHex('#cdcdcd'),
                          ),
                        ),
                        Positioned(
                          top: 0,
                          bottom: 0,
                          left: 9,
                          child: Align(
                            alignment: Alignment.center, // 让它居中
                            child: Container(
                              width: 6,
                              height: 6,
                              decoration: BoxDecoration(
                                color: Tools.fromHex('#cdcdcd'),
                                shape: BoxShape.circle, // 设置为圆形
                              ),
                            ),
                          ),
                        ),
                        Container(
                          width: ScreenUtil().screenWidth - 140,
                          margin: const EdgeInsets.all(10),
                          padding: const EdgeInsets.only(left: 10),
                          child: Text(
                            item['content'],
                            softWrap: true, // 显式设置为 true，允许换行
                          ),
                        ),
                      ],
                    )
                  ],
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  Widget _buildTopWidget() {
    return ClipRRect(
      child: Container(
        height: 120,
        margin: const EdgeInsets.all(10),
        decoration: const BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.all(Radius.circular(10)),
        ),
        child: Column(
          children: [
            Container(
              height: 60, // 设置高度，避免使用 Expanded
              alignment: Alignment.centerLeft,
              padding: const EdgeInsets.symmetric(horizontal: 20),
              child: Text(
                expressName,
                style: TextStyle(
                  color: Tools.fromHex('#333333'),
                ),
              ),
            ),
            Container(
              height: 60, // 设置高度，避免使用 Expanded
              padding: const EdgeInsets.symmetric(horizontal: 20),
              color: Tools.fromHex('#F5FCFF'),
              child: Row(
                children: [
                  Text(
                    '快递单号',
                    style: TextStyle(
                      color: Tools.fromHex('#616166'),
                    ),
                  ),
                  const SizedBox(width: 5),
                  Text(
                    expressCode,
                    style: TextStyle(
                      color: Tools.fromHex('#5094e5'),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
